package basis;

import java.util.Arrays;
import java.util.Scanner;

public class Base {
	
	static char[] ch = new char[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
		'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
		'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
		'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
		'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
		'8', '9', '+', '/'};

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入（编码）字符串");
		String s = sc.nextLine();
		// 编码
		encryotion(s);
		System.out.println("请输入（解码）字符串");
		String st = sc.next();
		System.out.println(Arrays.toString(decrypt(st)));

	}
	
	
	public static void encryotion(String s){
		// 转二进制
				String str = erJinZhi(s);
				System.out.println(str);
				// 分组（每六位）
				String[] arr = fenZu(str, 6);
				System.out.println(Arrays.toString(arr));
				// 将字符串数组转为int数组
				int[] bt = new int[arr.length];
				for (int i = 0; i < bt.length; i++) {
					bt[i] = Integer.parseInt(arr[i], 2);
				}
				// 在base64位进行查找。
				StringBuffer sb = new StringBuffer();
				for (int i = 0; i < bt.length; i++) {
					sb.append(ch[bt[i]]);
				}
				if(sb.length()%4!=0){
					for(int i=0;i<sb.length()%4;i++){
						sb.append('=');
					}
				}
				System.out.println("编码：" + sb.toString());

	}
	
	
	public static byte[] decrypt(String s){
		int[] bt = new int[s.length()];
		// 取base64编码字符角标
		for (int i = 0; i < ch.length; i++) {
			for (int j = 0; j < s.length(); j++) {
				if (s.charAt(j) == ch[i]) {
					bt[j] = i;
				}
			}
		}
		StringBuffer sb = new StringBuffer();
		// 将之转换为二进制
		String str1;
		for (int i = 0; i < bt.length; i++) {
			if (i != (bt.length - 1)) {
				// 最高位补0成6位（最后一个不补）
				str1 = buLing(Integer.toBinaryString(bt[i]), 6);
			} else {
				str1 = Integer.toBinaryString(bt[i]);
			}
			sb.append(str1);
		}
		String[] str2 = fenZu(sb.toString(), 8);
		byte [] bs= new byte[str2.length];
		for (int i = 0; i < str2.length; i++) {
			// 蒋二进制转十进制,int转换为字符
			byte a = (byte) Integer.parseInt(str2[i], 2);
			bs[i]=a;
		}
		return bs;

	}
	
	
	
	public static String erJinZhi(String s) {
		StringBuffer sb = new StringBuffer();
		byte[] bt = s.getBytes();
		for (int i = 0; i < bt.length; i++) {
			//
			String str = buLing(Integer.toBinaryString(bt[i]), 8);
			sb.append(str);
		}
		return sb.toString();
	}

	/**
	 * 每x位分组
	 * 
	 * @param s
	 * @param x
	 * @return
	 */
	public static String[] fenZu(String s, int x) {
		String str = "";
		int k = 0;
		int len = 0;
		String[] arr = new String[s.length()];

		// arr1数组存取分组
		// 计算arr1长度
		if (s.length() % x == 0) {
			len = s.length() / x;
		} else {
			len = s.length() / x + 1;
		}
		String[] arr1 = new String[len];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = s.charAt(i) + "";
		}
		for (int i = 0; i < arr.length; i++) {
			if (i % x == 0 && i != 0) {
				arr1[k] = str;
				str = "";
				k++;
			}
			str += arr[i];
		}
		if(str.length()<6){
			
			for(int i =str.length();i<6;i++){
				str+="0";
			}
		}
		arr1[arr1.length - 1] = str;
		//System.out.println(Arrays.toString(arr1));
		return arr1;
	}

	/**
	 * 将x的长度用0补为长度大小为y
	 * 
	 * @param x
	 *            需要补足长度的StringBuffer
	 * @param y
	 *            指定的长度
	 * @return
	 */
	public static String buLing(String x, int y) {
		StringBuffer sb = new StringBuffer();
		sb.append(x);
		sb = sb.reverse();
		for (int i = sb.length(); i < y; i++) {
			sb.append(0);
		}
		return sb.reverse().toString();
	}
}



